package com.google.code.stalkernet.node.event;

import java.util.EventObject;

import com.google.code.stalkernet.advertisement.Advertisement;
import com.google.code.stalkernet.peer.Peer;

/**
 * Encapsulates details of node-related event.
 *
 * @author Mikhail Vladimirov
 */
public class NodeEvent extends EventObject
{
    private final Peer peer;
    private final Advertisement advertisement;

    /**
     * Create new node event with given source and peer.
     *
     * @param source event source
     * @param peer peer
     */
    public NodeEvent (Object source, Peer peer)
    {
        super (source);

        if (peer == null)
            throw new IllegalArgumentException ("Peer is null");

        this.peer = peer;
        this.advertisement = null;
    }

    /**
     * Create new node event with given source and socket address.
     *
     * @param source event source
     * @param peer peer
     * @param advertisement advertisement
     */
    public NodeEvent (Object source, Peer peer, Advertisement advertisement)
    {
        super (source);

        if (peer == null)
            throw new IllegalArgumentException ("Peer is null");

        if (advertisement == null)
            throw new IllegalArgumentException ("Advertisement is null");

        this.peer = peer;
        this.advertisement = advertisement;
    }

    /**
     * Get peer of the event
     *
     * @return peer
     */
    public Peer getPeer ()
    {
        if (peer == null)
            throw new UnsupportedOperationException ("Peer is not available");

        return peer;
    }

    /**
     * Get advertisement of the event
     *
     * @return peer
     */
    public Advertisement getAdvertisement ()
    {
        if (advertisement == null)
            throw new UnsupportedOperationException (
                "Advertismeent is not available");

        return advertisement;
    }
}
